<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;

class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        //解析模板
        return view('home.index');
    }
    //登录
    public function login()
    {
       return view('home.login');
    }

    //执行登录操作
    public function doLogin(Request $request)
    {
        //表单验证
        $this->validate($request,[
          'username'=>'required',
          'password'=>'required'
          ],[
          'username.required'=>'请填写用户名',
          'password.required'=>'请填写密码'
          ]);
        //读取用户信息
        $res = DB::table('users')->where('username',$request->input('username'))->first();
        //匹配密码
        if(!$res){
            return back();
        }else{
            if(Hash::check($request->input('password'),$res->password)){
                //写入session
               session(['userid'=>$res->id,'username'=>$res->username,'userprofil'=>$res->profile]);
                return  redirect('/home');
            }else{
                return back();
            }
        }
    }

    //子页面一
     public function getPageone()
    {
       return view('home.pageone');
    }

      //子页面二
     public function getPagetwo()
    {
       return view('home.pagetwo');
    }

   
}
